//引入vuex
import store from '@/store/index.js'
import {
	mapGetters
} from 'vuex'

//引入websocket
import websocket from '@/utils/msg/websocketUtils.js'
//引入消息对象
import {
	msgHomeData,
	msgChatData
} from '@/object/msg.js'

/**
 * 1，连接上 websocket
 * 		port：端口号
 * 		clientId：设备ID（用于消息推送）
 */
export default {

	connectSocket(port) {
		/* const urlWss = 'ws://' + port + ':6003/netty/ws' */
		const urlWss = 'ws://' + port + ':18082/ws'
		//获取设备ID
		var clientId = '123'

		try {
			// #ifdef APP-PLUS
			if (uni.getStorageSync('isUniPush')) {
				const clientInfo = plus.push.getClientInfo();
				console.log("客户端标识：", clientInfo);
				clientId = clientInfo.clientid
			}
			// #endif
			// plus.push.getClientInfoAsync((info) => {
			// 	clientId = info["clientid"];
			// 	//console.log(cid)
			// });

			const wss = new websocket(urlWss, 5000, clientId)

			uni.myWebsocket = wss
			//消息接受监听
			wss.socketTask.onMessage((result) => {

				var res = JSON.parse(result.data)
				if (res.status !== 200) {
					uni.$showMsg(res.messageS)
					return
				}

				//1，连接上websocket
				if (res.type === 1) {
					//console.log(res)
					console.log(res.message)
					return;
				}

				var userId = uni.getStorageSync('userId')

				//var notDisList = store.getters('m_chat/notDisturbUserData')
				var isNotDisList = false

				//解析红包消息
				if (res.params.types == 21) {
					var redPkGInfo = JSON.parse(res.params.message)
					if (redPkGInfo.sendId != userId && redPkGInfo.robId != userId) {
						//不接受
						return;
					}
				}

				//console.log(res)

				//2，好友消息
				if (res.type == uni.$chatType.CHAT_FRIEND) {
					//console.log(res.params)
					var id = res.params.senderId
					var head = res.params.senderHead
					var name = res.params.senderName

					if (res.params.senderId == userId) {
						id = res.params.receiverId
						name = res.params.receiverName
						head = res.params.receiverHead
					}

					var addMsgHomeData = msgHomeData(
						id,
						2,
						name,
						head,
						res.params.message,
						res.params.time + '',
						1,
						res.params.types,
						res.params.id
					)
					//console.log(addMsgHomeData)

					var notDisturbUserData = store.state.m_chat.notDisturbUserData
					for (var i = 0; i < notDisturbUserData.length; i++) {
						if (notDisturbUserData[i].id == addMsgHomeData.id && notDisturbUserData[i].type ==
							addMsgHomeData.type) {
							console.log('消息免打扰')
							isNotDisList = true;
						}
					}

					if (res.params.senderId != userId && res.params.types != 21 && !isNotDisList) {
						uni.$isPromptPlay()
					}

					store.dispatch('m_chat/setMsgList', addMsgHomeData)

					//msgId, tip, news, sendId, sendHead, sendNickname, msgType, time
					var addMsgChatData = msgChatData(
						res.params.id,
						res.params.tip,
						res.params.message,
						res.params.senderId,
						res.params.senderHead,
						res.params.senderName,
						res.params.types,
						res.params.time + '',
						2,
						id,
						res.params.redPkgId
					)
					//console.log(addMsgChatData)
					store.dispatch('m_chat/addChatData', addMsgChatData)
					uni.$emit('getMessage',addMsgChatData)

				}
				//3，群聊消息
				if (res.type == uni.$chatType.CHAT_GROUP) {
					//console.log('console.log(res)')
					//console.log(res)
					if (res.params.types == 21) {
						//return
						var hbMsg = JSON.parse(res.params.message)
						if (hbMsg.robId != userId && hbMsg.sendId != userId) {
							console.log('不是我的接收消息')
							return
						}
					}

					//专属红包看看是不是发给我的
					if (res.params.types == 4) {
						//return
						//console.log(res.params)
						var hbMsg = JSON.parse(res.params.message)
						if (hbMsg.hbType == '3' && hbMsg.appointUserId != userId && res.params.senderId !=
							userId) {
							console.log('不是我的接收消息')
							return
						}

						// if (hbMsg.robId != userId && hbMsg.sendId != userId) {
						// 	console.log('不是我的接收消息')
						// 	return
						// }
					}



					var addMsgHomeData = msgHomeData(
						res.params.groupId,
						3,
						res.params.groupName,
						res.params.groupHead,
						res.params.message,
						res.params.time + '',
						1,
						res.params.types,
						res.params.id
					)

					var notDisturbUserData = store.state.m_chat.notDisturbUserData
					for (var i = 0; i < notDisturbUserData.length; i++) {
						if (notDisturbUserData[i].id == addMsgHomeData.id && notDisturbUserData[i].type ==
							addMsgHomeData.type) {
							console.log('消息免打扰')
							isNotDisList = true;
						}
					}

					if (res.params.senderId != userId && res.params.types != 21 && !isNotDisList) {
						uni.$isPromptPlay()
					}

					store.dispatch('m_chat/setMsgList', addMsgHomeData)

					//console.log(res.params)
					//msgId, tip, news, sendId, sendHead, sendNickname, msgType, time
					var addMsgChatData = msgChatData(
						res.params.id,
						res.params.tip,
						res.params.message,
						res.params.senderId,
						res.params.senderHead,
						res.params.senderName,
						res.params.types,
						res.params.time + '',
						3,
						res.params.groupId,
						res.params.redPkgId
					)

					store.dispatch('m_chat/addChatData', addMsgChatData)
					uni.$emit('getMessage',addMsgChatData)
				}
				//4，系统消息
				if (res.type == uni.$chatType.SYSTEM_MSG) {
					store.dispatch('m_chat/addsysNum')
				}

				return;
			});

		} catch (e) {
			//TODO handle the exception
		}
	}


}